package com.beiing.tianshuai.tianshuai.mine.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SimpleItemAnimator;
import android.view.View;
import android.widget.TextView;

import com.beiing.tianshuai.tianshuai.R;
import com.beiing.tianshuai.tianshuai.adapter.FansAndIdolAdapter;
import com.beiing.tianshuai.tianshuai.base.BaseFragment;
import com.beiing.tianshuai.tianshuai.entity.FollowResultBean;
import com.beiing.tianshuai.tianshuai.entity.MyAttentionBean;
import com.beiing.tianshuai.tianshuai.entity.UserInfoBean;
import com.beiing.tianshuai.tianshuai.mine.presenter.MyAttentionPresenter;
import com.beiing.tianshuai.tianshuai.mine.ui.NewPersonalIndexActivity;
import com.beiing.tianshuai.tianshuai.mine.view.MyAttentionViewImpl;
import com.beiing.tianshuai.tianshuai.util.DensityUtils;
import com.beiing.tianshuai.tianshuai.widget.RecycleViewDivider;

import java.util.List;

import butterknife.BindView;

/**
 * Created by Administrator on 2017/12/22 0022.
 */

public class FansAndIdolFragment extends BaseFragment implements MyAttentionViewImpl {

    @BindView(R.id.tv_empty_warning)
    TextView mEmptyWarning;
    @BindView(R.id.rv_fans_and_idol)
    RecyclerView mRvFansAndIdol;

    /**
     * presenter
     */
    private MyAttentionPresenter mPresenter;

    /**
     * 当前 fragment 所展示的数据类别 0-我关注的 1-关注我的
     */
    private int mPageType;

    /**
     * 列表适配器
     */
    private FansAndIdolAdapter mAdapter;

    /**
     * 网络请求重新请求次数
     */
    private int retryConnectionCount;

    /**
     * 所查看用户的id
     */
    private String mToUid;

    /**
     * 创建新的fragment
     *
     * @param type 类型 0-我关注的 1-关注我的
     * @return fragment
     */
    public static FansAndIdolFragment newInstance(int type, String toUid) {
        FansAndIdolFragment fragment = new FansAndIdolFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("type", type);
        bundle.putString("toUid", toUid);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_fans_and_idol;
    }

    @Override
    protected void init(View mView) {
        Bundle arguments = getArguments();
        if (arguments != null) {
            mPageType = arguments.getInt("type");
            mToUid = arguments.getString("toUid");
            initRecyclerView();
            initPresenter();
            initListener();
        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden && mPresenter != null) {
            requestData();
        }
    }

    /**
     * 初始化 RecyclerView
     */
    private void initRecyclerView() {

        // 设置layoutManager
        LinearLayoutManager manager = new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false);
        mRvFansAndIdol.setLayoutManager(manager);
        mRvFansAndIdol.addItemDecoration(
                new RecycleViewDivider(
                        mContext, LinearLayoutManager.HORIZONTAL, DensityUtils.dp2px(mContext, 0.5f),
                        ContextCompat.getColor(mContext, R.color.common_gray_background))
        );
        ((SimpleItemAnimator) mRvFansAndIdol.getItemAnimator()).setSupportsChangeAnimations(false);

        // 设置适配器
        mAdapter = new FansAndIdolAdapter(mContext, mPageType);
        mRvFansAndIdol.setAdapter(mAdapter);
    }

    /**
     * 初始化 presenter 并且请求数据
     */
    private void initPresenter() {
        mPresenter = new MyAttentionPresenter(this);
        requestData();
    }

    /**
     * 初始化监听事件
     */
    private void initListener() {
        mAdapter.setOnCancelFollowClickListener(new FansAndIdolAdapter.OnCancelFollowClickListener() {
            @Override
            public void onCancelFollow(MyAttentionBean.DataBean.ConcernBean bean, int position) {
                mPresenter.requestFollow(UserInfoBean.getUid(mContext), bean, position, mPageType);
            }
        });

        mAdapter.setOnFollowClickListener(new FansAndIdolAdapter.OnFollowClickListener() {
            @Override
            public void onFollow(MyAttentionBean.DataBean.ConcernBean bean, int position) {
                mPresenter.requestFollow(UserInfoBean.getUid(mContext), bean, position, mPageType);
            }
        });

        mAdapter.setOnItemClickListener(new FansAndIdolAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(MyAttentionBean.DataBean.ConcernBean bean, int position) {
                Intent intent = new Intent(mContext, NewPersonalIndexActivity.class);
                switch (mPageType) {
                    case 0:
                        intent.putExtra("uid", bean.getToUid());
                        break;
                    case 1:
                        intent.putExtra("uid", bean.getFromUid());
                        break;
                }
                startActivity(intent);
            }
        });
    }

    /**
     * 请求列表数据
     */
    private void requestData() {
        switch (mPageType) {
            case 0:
                mPresenter.getMyAttentionResult(mToUid);
                break;
            case 1:
                mPresenter.getMyFansResult(mToUid);
                break;
        }
    }

    @Override
    public void showProgress() {

    }

    @Override
    public void hideProgress() {

    }

    @Override
    public void onRequestSuccess(MyAttentionBean bean) {
        if (bean.getCode() == 200) {
            List<MyAttentionBean.DataBean.ConcernBean> beanList = bean.getData().getConcern();
            if (!beanList.isEmpty()) {
                if (mEmptyWarning != null) {
                    mEmptyWarning.setVisibility(View.GONE);
                }
                mAdapter.updateData(beanList);
            } else {
                if (mEmptyWarning != null) {
                    mEmptyWarning.setVisibility(View.VISIBLE);
                }
            }
        }
    }

    @Override
    public void onRequestFailed(Throwable throwable) {
        if (retryConnectionCount < 5) {
            retryConnectionCount++;
            requestData();
        } else {
            retryConnectionCount = 0;
            showShortText("请求失败");
        }
    }

    @Override
    public void onFollowSuccess(FollowResultBean bean, int position, MyAttentionBean.DataBean.ConcernBean concernBean) {
        if (bean.getCode() == 204 && mPageType == 0) {
            mAdapter.removeData(position);
            int size = mAdapter.getList().size();
            mAdapter.notifyItemChanged(size);
            if (size == 0) {
                mEmptyWarning.setVisibility(View.VISIBLE);
            }
        } else if (bean.getCode() == 204 && mPageType == 1) {
            concernBean.setM(0);
            mAdapter.notifyItemChanged(position);
        } else if (bean.getCode() == 203 && mPageType == 1) {
            concernBean.setM(1);
            mAdapter.notifyItemChanged(position);
        }
    }

    @Override
    public void onFollowError(Throwable throwable) {
        showShortText("操作失败");
    }
}
